<?

class ProjectController extends Controller{
    
    function actionIndex(){
        $this->actionList();
    }
    
    function actionList(){
        
        $accessDenied = !TaskUser::UserInProject();
        $projects = array();
        
        if($accessDenied && (Yii::app()->user->checkAccess('developer') || Yii::app()->user->checkAccess('client'))){
            $PR = TaskProjectUser::model()->findAllByAttributes(array(
                'user_id' => Yii::app()->user->id
            ));
            foreach($PR as $P){
                array_push($projects, $P->project_id);
            }
            if(!empty($projects))
                $accessDenied = false;
        }
        
        if($accessDenied){
            throw new CHttpException(403,'Forbidden');
        }
        
        $this->pageTitle = TaskModule::t('app', 'Проекты');
        $this->breadcrumbs[] = TaskModule::t('app', 'Проекты');
        
        $criteria = new CDbCriteria;
        if(!empty($projects)) $criteria->addInCondition('`t`.`id`', $projects);
        $criteria->order = '`t`.`code` ASC';
        $PROJECTS = new CActiveDataProvider(TaskProject::model()->with('users'), array(
            'pagination'=>array('pageSize'=>10),
            'criteria'=>$criteria,
        ));
        
        $this->render('list', array(
            'PROJECTS' => $PROJECTS
        ));
        
    }
    
    function actionView(){
        
        $_CODE = trim(substr(strip_tags($_GET['code']), 0, 60));
        
        if(empty($_CODE)){
            $this->redirect($this->createUrl('project/list'));
        }
        
        $PROJECT = TaskProject::findByCode($_CODE, 'users.user');
        if(!$PROJECT){
            Yii::app()->user->setFlash(FL_ERROR, TaskModule::t('messages', 'Проект «'.$_CODE.'» не найден.'));
        }else{
            
            if(!TaskUser::UserInProject($PROJECT->id)){
                throw new CHttpException(403,'Forbidden');
            }
            
            $this->pageTitle = 'Проект «'.$PROJECT->name.'»';
            $this->breadcrumbs[TaskModule::t('app', 'Проекты')] = $this->createUrl('project/list');
            $this->breadcrumbs[] = $PROJECT->name;
            
            $USERS = $PROJECT->GetRelated('users');
            $PROJECTUSERS = array();
            
            $projectMembers = array();
            foreach($USERS as $U){
                $user = $U->GetRelated('user');
                $PROJECTUSERS[] = $user;
                $userurl = $this->createUrl('/user/show/member/', array('id'=>$user->id));
                array_push($projectMembers, '<a href="'.$userurl.'">'.$user->lname.' '.$user->name.'</a>');
            }
            
            if(isset($_POST['username'])){
                if(!preg_match('|\[(\d)+\]|is', $_POST['username'], $m)){
                    Yii::app()->user->setFlash(FL_ERROR, TaskModule::t('messages', 'Пользователь не найден.'));
                }else{
                    $uid = intval($m[1]);
                    $already = false;
                    foreach($PROJECTUSERS as $PU){
                        if($PU->id == $uid){
                            $already = true; break;
                        }
                    }
                    if($already){
                        Yii::app()->user->setFlash(FL_NOTICE, TaskModule::t('messages', 'Пользователь уже учавствует в проекте.'));
                    }else{
                        
                        $TPU = new TaskProjectUser;
                        $TPU->user_id = $uid;
                        $TPU->project_id = $PROJECT->id;
                        $TPU->save();
                        
                        Yii::app()->user->setFlash(FL_SUCCESS, TaskModule::t('messages', 'Пользователь успешно стал участником проекта.'));
                        
                    }
                }
                $this->refresh();
            }
            
        }
        
        $this->render('view', array(
            'PROJECT' => $PROJECT,
            'FORM_ADDMEMBER' => $FORM_ADDMEMBER,
            'projectMembers' => $projectMembers,
        ));
        
    }
    
}
